package cn.gdcp.volleyrequest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
private Button btn1;
private Button btn2;
private TextView txt_result;
private RequestQueue stringRequest;
private static final String TAG = "MainActivity";
private TextView cast_1;
private TextView cast_2;
private TextView cast_3;
private ImageView img1;
private ImageView img2;
private ImageView img3;
private JSONArray jsonArray1;
private ListView broadCast;
private ArrayList<StringData> arrayList=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initBundler();
        btn1Event();
        btn2Event();
    }

    private void btn2Event() {
                            btn2.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    userJSONRequest();
                                }
                            });
    }

    private void userJSONRequest() {
        String url="http://t.weather.sojson.com/api/weather/city/101280101";
        JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                try {

                    JSONObject jsonObject1=jsonObject.getJSONObject("data");
                    JSONArray jsonArray=jsonObject1.getJSONArray("forecast");

                        jsonArray1=new JSONArray();
                        int i=0;
                        while (i<3){
                            jsonArray1.put(jsonArray.get(i));
                            i++;
                        }
                        JSONObject j1= (JSONObject) jsonArray1.get(0);
                        JSONObject j2= (JSONObject) jsonArray1.get(1);
                        JSONObject j3= (JSONObject) jsonArray1.get(2);

                        String type1=j1.getString("type");
                         String ymd1=j1.getString("ymd");
                        Log.e(TAG, "onResponse: "+type1 );

                        String type2=j2.getString("type");
                        String ymd2=j2.getString("ymd");

                        String type3=j3.getString("type");
                        String ymd3=j3.getString("ymd");

                        StringBuffer stringBuffer1=new StringBuffer();
                        StringBuffer stringBuffer2=new StringBuffer();
                        StringBuffer stringBuffer3=new StringBuffer();
                        stringBuffer1.append("天气："+type1+"\n");
                        stringBuffer1.append("日期："+ymd1+"\n");

                        stringBuffer2.append("天气："+type2+"\n");
                        stringBuffer2.append("日期："+ymd2+"\n");

                        stringBuffer3.append("天气："+type3+"\n");
                        stringBuffer3.append("日期："+ymd3+"\n");

                        cast_1.setText(stringBuffer1.toString());
                        cast_2.setText(stringBuffer2.toString());
                        cast_3.setText(stringBuffer3.toString());
                            switch (type1) {
                                case "晴":
                                    img1.setImageResource(R.drawable.ca1);
                                    break;
                                case "阴":
                                    img1.setImageResource(R.drawable.ca3);
                                    break;
                                case "多云":
                                    img1.setImageResource(R.drawable.ca4);
                                    break;
                            }

                    switch (type2) {
                        case "晴":
                            img2.setImageResource(R.drawable.ca1);
                            break;
                        case "阴":
                            img2.setImageResource(R.drawable.ca3);
                            break;
                        case "多云":
                            img2.setImageResource(R.drawable.ca4);
                            break;
                    }

                    switch (type3) {
                        case "晴":
                            img3.setImageResource(R.drawable.ca1);
                            break;
                        case "阴":
                            img3.setImageResource(R.drawable.ca3);
                            break;
                        case "多云":
                            img3.setImageResource(R.drawable.ca4);
                            break;
                    }


                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e(TAG, "onErrorResponse: "+volleyError.toString() );

            }
        });

           stringRequest.add(jsonObjectRequest);

    }

    private void btn1Event() {
                  btn1.setOnClickListener(new View.OnClickListener() {
                      @Override
                      public void onClick(View view) {
                         /* useStringRequeset("15876745586");*/
                          userPostStringRequest("15876745586");
                      }
                  });
    }
    private void userPostStringRequest(final String s) {
        String phoneBaseUrl="http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo";
   /*     String phoneUrl=phoneBaseUrl.concat("?mobileCode=").concat(s).concat("&userID=");*/
        StringRequest VolleystringRequest=new StringRequest(Request.Method.POST,phoneBaseUrl, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                Log.e(TAG, "内容："+s );
                txt_result.setText(s);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e(TAG, "onErrorResponse: "+volleyError.toString() );
            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> map=new HashMap<>();
                map.put("mobileCode",s);
                map.put("userID","");
                return map;
            }
        };
        stringRequest.add(VolleystringRequest);
    }
    private void useStringRequeset(String s) {
                           String phoneBaseUrl="http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo";
                           String phoneUrl=phoneBaseUrl.concat("?mobileCode=").concat(s).concat("&userID=");
                           StringRequest VolleystringRequest=new StringRequest(phoneUrl, new Response.Listener<String>() {
                               @Override
                               public void onResponse(String s) {
                                   Log.e(TAG, "内容："+s );
                                   txt_result.setText(s);
                               }
                           }, new Response.ErrorListener() {
                               @Override
                               public void onErrorResponse(VolleyError volleyError) {
                                   Log.e(TAG, "onErrorResponse: "+volleyError.toString() );
                               }
                           });
                           stringRequest.add(VolleystringRequest);
    }
    private void initBundler() {
        stringRequest= Volley.newRequestQueue(MainActivity.this);
                   btn1=findViewById(R.id.btn1);
                   btn2=findViewById(R.id.btn2);
                   txt_result=findViewById(R.id.txt_result);
                   img1=findViewById(R.id.img1);
                   img2=findViewById(R.id.img2);
                   img3=findViewById(R.id.img3);
                   cast_1=findViewById(R.id.cast_txt1);
                   cast_2=findViewById(R.id.cast_txt2);
                   cast_3=findViewById(R.id.cast_txt3);
                   broadCast=findViewById(R.id.broadcast_list);
     }
}
